package com.dilidili.controller;

import com.dilidili.service.ICommentService;
import com.dilidili.service.IUserService;
import com.domain.Comment;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-03-31
 */
@RestController
@RequestMapping("/system/comment")
public class CommentController
{
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserService userService;
    @RequestMapping("/ds")
    public void deleteBatch(Long[] ids)
    {
        for (Long id : ids) {
            delete(id);
        }
//        commentService.deleteCommentByCommentIds(ids);
    }
    @RequestMapping("/d")
    public void delete(Long id){

        List<Comment> comments=commentService.getByCommentId(id);
        if(comments.size()<1){
            return;
        }
        for (Comment comment : comments) {
            delete(comment.getCommentId());
            commentService.deleteCommentByCommentId(comment.getCommentId());
        }
        commentService.deleteCommentByCommentId(id);
    }
    @RequestMapping("/add")
    public void add(Comment comment){
        commentService.insertComment(comment);
    }

    //评论管理
    @RequestMapping("/manageReviews")
    public Object manageReviews(@RequestParam(defaultValue = "", required = false) String videoTitle, Integer userId,
                                @RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                @RequestParam(defaultValue = "5", required = false) Integer pageSize)
    {
        PageHelper.startPage(pageNum, pageSize);
        List<Comment> submission = commentService.selectCommentByCommentId2(userId,videoTitle);
        PageInfo pageInfo = new PageInfo(submission);
        return pageInfo;
    }

    /**
     *
     * @param videoId
     * @return
     */
    @RequestMapping("/getComList")
    public Object getcomList(Integer videoId){
        Long vid=Long.parseLong(videoId+"");
        List<Map> maps = commentService.getcomList(vid,0L);
        List<Map> list=new ArrayList<>();
        for (Map map1 : maps) {
            Long comId= (Long) map1.get("comment_user_id");
            Map<String,Object> map2=userService.getComUserById(comId);
            map1.put("comUser",map2);
            Long pid=Long.parseLong(map1.get("comment_id")+"");
            List<Map> comChildrenList = getComChildrenList(list, pid, vid);
            if(comChildrenList.size()>0&&comChildrenList!=null) {
                map1.put("comChildren", comChildrenList);
            }
        }
        return maps;
    }
    //查看子评论
    @RequestMapping("/getComChildrenList")
    public  List<Map>  getComChildrenList(List<Map> lists,Long pid,Long videoId){
        if(lists==null){
            lists=new ArrayList<>();
        }
        List<Map> maps = commentService.getcomList(videoId,pid);
        for (Map map : maps) {
            Long comId= (Long) map.get("comment_user_id");
            Map<String,Object> map1=userService.getComUserById(comId);
            map.put("comUser",map1);
            Long compid= Long.parseLong(map.get("comment_parentid")+"");
            Map<String,Object> map2=userService.getComPuserByPid(compid);
            map.put("comPuser",map2);
            lists.add(map);
            Long pid1=Long.parseLong(map.get("comment_id")+"");
            getComChildrenList(lists,pid1,videoId);
        }
        return lists;
    }
    @RequestMapping("/sendComment")
    public Object sendComment(Comment comment){
        return "1";
    }


}
